package com.mingtu.jgspatrol.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hjq.gson.factory.GsonFactory;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.base.BaseBindingFragment;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.IntentUtils;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.activity.NewsDetailsActivity;
import com.mingtu.jgspatrol.adapter.NavAdapter2;
import com.mingtu.jgspatrol.bean.NoticeBean;
import com.mingtu.jgspatrol.databinding.FragmentNav2Binding;
import com.mingtu.jgspatrol.utils.MyConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Nav2Fragment extends BaseBindingFragment<FragmentNav2Binding> implements LazyFragmentPagerAdapter.Laziable {
    private List<NoticeBean.PageBean.ListBean> DataBean = new ArrayList<>();
    private NavAdapter2 navAdapter2;
    private int currentPage = 1;
    private boolean isLoadMore = true;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_nav2;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext().getApplicationContext());
        binding.recycler.setLayoutManager(linearLayoutManager);

        //添加自定义分割线
        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.shape_dive_line));
        binding.recycler.addItemDecoration(divider);

        navAdapter2 = new NavAdapter2(context, DataBean);
        binding.recycler.setAdapter(navAdapter2);
        navAdapter2.setOnItemClickListener(new NavAdapter2.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                String id = DataBean.get(position).getId() + "";
                Bundle bundle = new Bundle();
                bundle.putString("id", id);
                IntentUtils.getInstance().readyGo(getActivity(), NewsDetailsActivity.class, bundle);
            }
        });
    }

    @Override
    protected void getData() {
        getNoticeList(1);

    }

    public void getNoticeList(int pageNum) {
        if (pageNum == 1) {
            currentPage = 1;
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("curPage", pageNum);
        hashMap.put("pageSize", Constant.PAGE_SIZE);
        hashMap.put("contentType", 7);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_NEWS_LIST)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            try {
                                Gson gson = GsonFactory.getSingletonGson();
                                NoticeBean messageBean = new NoticeBean();
                                messageBean = gson.fromJson(result, NoticeBean.class);
                                List<NoticeBean.PageBean.ListBean> dataBeans = messageBean.getPage().getList();
                                try {
                                    binding.emptyLayout.setIsShowView(false);
                                    binding.recycler.setVisibility(View.VISIBLE);


                                    if (dataBeans != null) {
                                        isLoadMore = dataBeans.size() > 0 && dataBeans.size() % Constant.PAGE_SIZE == 0 ? true : false;
                                        ((IndexFragment) (Nav2Fragment.this.getParentFragment())).getIsLoadMoreArray()[1] = isLoadMore;
                                        if (!isLoadMore) {
                                            if (currentPage > 1) {
                                                ToastUtils.showLong(getResources().getString(com.mingtu.center.R.string.text_no_more_data));
                                            } else {
                                                if (dataBeans.size() == 0) {
                                                    //显示占位图
                                                    binding.emptyLayout.setIsShowView(true);
                                                    binding.recycler.setVisibility(View.GONE);
                                                }

                                            }
                                        }
                                        //当是第一页的时候先清空数组，防止下拉操作的时候加载重复数据
                                        if (currentPage == 1) {
                                            navAdapter2.clearData();
                                        }

                                        DataBean.addAll(dataBeans);
                                        navAdapter2.upData(DataBean);
                                        ((IndexFragment) (Nav2Fragment.this.getParentFragment())).setIndexNoticeDataBean(DataBean);
                                    } else {
                                        if (currentPage == 1) {
                                            //显示占位图
                                            binding.recycler.setVisibility(View.GONE);
                                            binding.emptyLayout.setIsShowView(true);
                                        }
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            } catch (JsonSyntaxException e) {
                                e.printStackTrace();
                                isLoadMore = false;
                                //显示占位图
                                binding.recycler.setVisibility(View.GONE);
                                binding.emptyLayout.setIsShowView(true);
                                ToastUtils.showLong(getResources().getString(com.mingtu.center.R.string.text_json_error));
                            }


                        }
                    }
                });

    }


    /**
     * 刷新数据
     */
    public void refresh() {
        getNoticeList(1);
    }


}